package com.molichuxing.gateway.api.services.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.gateway.api.config.OptionalLogConfig;
import com.molichuxing.gateway.api.services.OrderEnterpriseApiService;
import com.molichuxing.gateway.bff.order.entity.request.create.*;
import com.molichuxing.gateway.bff.order.entity.response.*;
import com.molichuxing.gateway.bff.order.services.EnterpriseCarTransferBffService;
import com.molichuxing.gateway.bff.order.services.OrderEnterpriseBffService;
import com.molichuxing.gateway.bff.order.services.OrderEnterpriseCarHandoverBffService;
import com.molichuxing.gateway.bff.order.services.OrderEnterpriseScheduleBffService;
import com.molichuxing.gateway.utils.TokenUtil;
import com.molichuxing.services.infrastructure.dto.response.EnterpriseScheduleDto;
import com.molichuxing.services.infrastructure.service.EnterpriseScheduleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 企业订单相关接口
 *
 * @Author zoumingyu
 * @Date 2020年06月03日
 */
@Api(tags = "企业订单相关接口")
@RestController
@RequestMapping(value = "/order/enterprise")
public class OrderEnterpriseApiServiceImpl extends RootApiService implements OrderEnterpriseApiService {
    private static final Logger logger = LoggerFactory.getLogger(OrderApiServiceImpl.class);

    @Resource
    private TokenUtil tokenUtil;

    @Resource
    private OrderEnterpriseBffService orderEnterpriseBffService;

    @Resource
    private OrderEnterpriseScheduleBffService orderEnterpriseScheduleBffService;

    @Resource
    private EnterpriseCarTransferBffService enterpriseCarTransferBffService;

    @Resource
    private OrderEnterpriseCarHandoverBffService orderEnterpriseCarHandoverBffService;

    @Resource
    EnterpriseScheduleService enterpriseScheduleService;

    @Override
    @ApiOperation(value = "企业订单流转")
    @GetMapping(value = "/state/flow/list")
    public ResponseResult<List<OrderTradeFlowVo>> OrderEnterpriseStateFlow(Long orderCode) {
        ResponseResult<List<OrderTradeFlowVo>> result = new ResponseResult<>();
        try {
            result.success(orderEnterpriseBffService.getEnterpriseStateFlow(orderCode));
        } catch (BizException e) {
            logger.error("查询订单状态流异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("查询订单状态流异常:", e);
            result.error("查询订单状态流异常");
        }

        return result;
    }

    @Override
    @ApiOperation(value = "租赁车辆列表")
    @ApiImplicitParams({ @ApiImplicitParam(name = "state", value = "订单类型", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "orderCode", value = "订单号", required = false, dataType = "Long"),
            @ApiImplicitParam(name = "vin", value = "车架号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "licencePlate", value = "车牌号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "enterpriseId", value = "企业ID", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "leaseInceptionStart", value = "起租日开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "leaseInceptionEnd", value = "起租日结束时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "leaseExpireStart", value = "租赁截至日开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "leaseExpireEnd", value = "租赁截至日结束时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "returnedTimeStart", value = "退租开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "returnedTimeEnd", value = "退租结束时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "transferTimeStart", value = "过户开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "transferTimeEnd", value = "过户结束时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/car/list")
    public ResponseResult<Paged<OrderEnterpriseCarBizVo>> orderEnterpriseCarList(@RequestParam Map<String, Object> params,
                                                                            @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        ResponseResult<Paged<OrderEnterpriseCarBizVo>> result = new ResponseResult<>();
        try {
            result.success(orderEnterpriseBffService.orderEnterpriseCarList(params, pageNum, pageSize));
        } catch (Exception e) {
            // 记录异常，返回异常信息
            logger.error("[orderDealerList]获取经销商订单列表异常", e);
            result.error("获取经销商订单列表异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "企业订单列表")
    @ApiImplicitParams({ @ApiImplicitParam(name = "state", value = "订单类型", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "orderCode", value = "订单号", required = false, dataType = "Long"),
            @ApiImplicitParam(name = "enterpriseId", value = "企业ID", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "leaseInceptionStart", value = "起租日开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "leaseInceptionEnd", value = "起租日结束时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "leaseExpireStart", value = "租赁截至日开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "leaseExpireEnd", value = "租赁截至日结束时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "gmtCreateStart", value = "创建开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "gmtCreateEnd", value = "创建结束时间", required = false, dataType = "String")
    })
    @GetMapping(value = "/list")
    public ResponseResult<Paged<OrderEnterpriseListVo>> orderEnterpriseList(@RequestParam Map<String, Object> params,
                                                                            @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        ResponseResult<Paged<OrderEnterpriseListVo>> result = new ResponseResult<>();
        try {
            result.success(orderEnterpriseBffService.orderEnterpriseList(params, pageNum, pageSize));
        } catch (Exception e) {
            // 记录异常，返回异常信息
            logger.error("[orderDealerList]获取经销商订单列表异常", e);
            result.error("获取经销商订单列表异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "企业订单基本信息")
    @ApiImplicitParam(name = "orderCode", value = "订单号", required = true, dataType = "Long")
    @GetMapping(value = "/base")
    public ResponseResult<OrderEnterpriseBaseVo> getEnterpriseBase(@RequestParam Long orderCode) {
        ResponseResult<OrderEnterpriseBaseVo> result = new ResponseResult<>();
        try {
            result.success(orderEnterpriseBffService.getEnterpriseBase(orderCode));
        } catch (BizException e) {
            logger.error("[getEnterpriseBase]查询企业订单基本信息异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[getEnterpriseBase]查询企业订单基本信息异常:", e);
            result.error("查询企业订单基本信息异常");
        }

        return result;
    }

    @Override
    @ApiOperation(value = "企业订单车型车辆列表")
    @ApiImplicitParam(name = "orderCode", value = "订单号", required = true, dataType = "Long")
    @GetMapping(value = "/order/model/car")
    public ResponseResult<List<OrderEnterpriseModelCarVo>> getOrderEnterpriseModelCar(@RequestParam Long orderCode) {
        ResponseResult<List<OrderEnterpriseModelCarVo>> result = new ResponseResult<>();
        try {
            result.success(orderEnterpriseBffService.getOrderEnterpriseModelCar(orderCode));
        } catch (BizException e) {
            logger.error("查询企业订单车型车辆列表异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("查询企业订单车型车辆列表异常:", e);
            result.error("查询企业订单车型车辆列表异常");
        }

        return result;
    }

    @Override
    @ApiOperation(value = "企业订单车辆明细")
    @ApiImplicitParam(name = "orderCode", value = "订单号", required = true, dataType = "Long")
    @GetMapping(value = "/car")
    public ResponseResult<OrderEnterpriseCarDetailVo> getEnterpriseCar(@RequestParam Long orderCode) {
        ResponseResult<OrderEnterpriseCarDetailVo> result = new ResponseResult<>();
        try {
            result.success(orderEnterpriseBffService.getEnterpriseCar(orderCode));

        } catch (BizException e) {
            logger.error("[getEnterpriseCar]查询企业订单车辆明细异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[getEnterpriseCar]查询企业订单车辆明细异常:", e);
            result.error("查询企业订单车辆明细异常");
        }

        return result;
    }

    @Override
    @ApiOperation(value = "企业订单交车信息")
    @ApiImplicitParam(name = "orderCode", value = "订单号", required = true, dataType = "Long")
    @GetMapping(value = "/car/delivery")
    public ResponseResult<List<OrderEnterpriseCarHandoverVo>> getEnterpriseCarDelivery(@RequestParam Long orderCode) {
        ResponseResult<List<OrderEnterpriseCarHandoverVo>> result = new ResponseResult<>();
        try {
            result.success(orderEnterpriseBffService.getEnterpriseCarDelivery(orderCode));
        } catch (BizException e) {
            logger.error("[getEnterpriseCarDelivery]查询企业订单交车信息异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[getEnterpriseCarDelivery]查询企业订单交车信息异常:", e);
            result.error("查询企业订单交车信息异常");
        }

        return result;
    }

    @Override
    @ApiOperation(value = "企业订单还款信息")
    @ApiImplicitParam(name = "orderCode", value = "订单号", required = true, dataType = "Long")
    @GetMapping(value = "/repayment")
    public ResponseResult<OrderEnterpriseRepaymentDetailVo> getEnterpriseRepayment(@RequestParam Long orderCode) {
        ResponseResult<OrderEnterpriseRepaymentDetailVo> result = new ResponseResult<>();
        try {
            result.success(orderEnterpriseBffService.getEnterpriseRepayment(orderCode));
        } catch (BizException e) {
            logger.error("[getEnterpriseRepayment]查询企业订单还款信息异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[getEnterpriseRepayment]查询企业订单还款信息异常:", e);
            result.error("查询企业订单还款信息异常");
        }

        return result;
    }

    @Override
    @ApiOperation(value = "获取还款管理列表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tabType", value = "营业状态（1：还款计划，2：逾期未还款，3：支付记录）", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "orderCode", value = "订单号", required = false, dataType = "Long"),
            @ApiImplicitParam(name = "enterpriseId", value = "企业ID", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "state", value = "还款状态（0：未还款，1：已还款）", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "payableDateStart", value = "应还款日开始时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "payableDateEnd", value = "应还款日结束时间", required = false, dataType = "String"),
            @ApiImplicitParam(name = "sortName", value = "排序字段", required = false, dataType = "String"),
            @ApiImplicitParam(name = "sortWay", value = "排序方式 asc:正序，desc:倒叙", required = false, dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/schedule/list")
    public ResponseResult<Paged<EnterpriseScheduleVo>> getEnterpriseSchedulePage(@RequestParam Map<String, Object> params,
                                                                                 @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        ResponseResult<Paged<EnterpriseScheduleVo>> result = new ResponseResult<>();
        try {
            Object payableDateStart = params.get("payableDateStart");
            Object payableDateEnd = params.get("payableDateEnd");
            if (payableDateStart != null) {
                LocalDateTime dayStart = ObjectCastUtil.getDayStart(payableDateStart);
                params.put("payableDateStart", dayStart);
            }
            if (payableDateEnd != null) {
                LocalDateTime dayEnd = ObjectCastUtil.getDayEnd(payableDateEnd);
                params.put("payableDateEnd", dayEnd);
            }

            return result.success(orderEnterpriseScheduleBffService.getEnterpriseSchedulePage(params, pageNum, pageSize));
        } catch (BizException e) {
            logger.error("[getEnterpriseSchedulePage]获取还款管理列表信息异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[getEnterpriseSchedulePage]获取还款管理列表信息异常:", e);
            result.error("获取还款管理列表信息异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "还款管理-详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "scheduleId", value = "排期ID", required = true, dataType = "Integer"),
    })
    @GetMapping(value = "/schedule/detail")
    public ResponseResult<PaymentEnterpriseInfoVo> getPaymentEnterpriseInfo(@RequestParam Integer scheduleId) {
        ResponseResult<PaymentEnterpriseInfoVo> result = new ResponseResult<>();
        try {
            return result.success(orderEnterpriseScheduleBffService.getPaymentEnterpriseInfo(scheduleId));
        } catch (BizException e) {
            logger.error("[getPaymentEnterpriseInfo]获取详情异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[getPaymentEnterpriseInfo]获取详情异常:", e);
            result.error("获取详情异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "退租")
    @PostMapping(value = "/car/return/create")
    @OptionalLogConfig(methods = "退租")
    public ResponseResult<Boolean> createOrderEnterpriseReturn(@RequestBody @Valid OrderEnterpriseReturnCreateVo orderEnterpriseReturnCreateVo,
                                                               BindingResult bindingResult) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        try {
            return result.success(orderEnterpriseCarHandoverBffService.createOrderEnterpriseReturn(orderEnterpriseReturnCreateVo));
        } catch (BizException e) {
            logger.error("[createOrderEnterpriseReturn]退租异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[createOrderEnterpriseReturn]退租异常:", e);
            result.error("退租异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "退租-详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "carHandoverId", value = "退租id", required = true, dataType = "Integer"),
    })
    @GetMapping(value = "/car/handover/return/detail")
    public ResponseResult<OrderEnterpriseCarReturnVo> getOrderEnterpriseReturnDetail(@RequestParam Integer carHandoverId) {
        ResponseResult<OrderEnterpriseCarReturnVo> result = new ResponseResult<>();
        try {
            return result.success(orderEnterpriseCarHandoverBffService.getOrderEnterpriseReturnDetail(carHandoverId));
        } catch (BizException e) {
            logger.error("[getOrderEnterpriseReturnDetail]获取详情异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[getOrderEnterpriseReturnDetail]获取详情异常:", e);
            result.error("获取详情异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "交车")
    @PostMapping(value = "/car/delivery/create")
    @OptionalLogConfig(methods = "交车")
    public ResponseResult<Boolean> createOrderEnterpriseDelivery(@RequestBody @Valid OrderEnterpriseDeliveryCreateVo orderEnterpriseDeliveryCreateVo,
                                                                 BindingResult bindingResult) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        try {
            return result.success(orderEnterpriseCarHandoverBffService.createOrderEnterpriseDelivery(orderEnterpriseDeliveryCreateVo));
        } catch (BizException e) {
            logger.error("[createOrderEnterpriseDelivery]交车异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[createOrderEnterpriseDelivery]交车异常:", e);
            result.error("交车异常");
        }
        return result;
    }


    @Override
    @ApiOperation(value = "新增企业订单")
    @PostMapping(value = "/create")
    @OptionalLogConfig(methods = "新增企业订单")
    public ResponseResult<Boolean> create(@RequestBody @Valid OrderEnterpriseCreateVo createVo,
                                          BindingResult bindingResult) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        try {
            boolean rst = orderEnterpriseBffService.create(createVo);
            result = rst ? result.success(rst) : result.error("新增企业订单失败");
        } catch (BizException e) {
            logger.error("[create]新增企业订单异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[create]新增企业订单异常", e);
            result.error("新增企业订单失败");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "新增企业订单续租")
    @PostMapping(value = "/create/renew")
    @OptionalLogConfig(methods = "新增企业订单续租")
    public ResponseResult<Boolean> createRenew(@RequestBody @Valid OrderEnterpriseCreateVo createVo,
                                               BindingResult bindingResult) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        try {
            boolean rst = orderEnterpriseBffService.create(createVo);
            result = rst ? result.success(rst) : result.error("新增企业订单续租失败");
        } catch (BizException e) {
            logger.error("[createRenew]新增企业订单续租异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[createRenew]新增企业订单续租异常", e);
            result.error("新增企业订单续租失败");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "企业车辆过户")
    @PostMapping(value = "/transfer/create")
    @OptionalLogConfig(methods = "企业车辆过户")
    public ResponseResult<Boolean> createEnterpriseCarTransfer(@RequestBody @Valid EnterpriseCarTransferCreateVo createVo,
                                                               BindingResult bindingResult) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        try {
            boolean rst = enterpriseCarTransferBffService.create(createVo);
            result = rst ? result.success(rst) : result.error("企业车辆过户失败");
        } catch (BizException e) {
            logger.error("[createEnterpriseCarTransfer]企业车辆过户异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[createEnterpriseCarTransfer]企业车辆过户异常", e);
            result.error("企业车辆过户失败");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "企业车辆过户详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "vin", value = "车架号", required = true, dataType = "String"),
    })
    @GetMapping(value = "/transfer/detail")
    public ResponseResult<EnterpriseCarTransferVo> getEnterpriseCarTransfer(String vin) {
        ResponseResult<EnterpriseCarTransferVo> result = new ResponseResult<>();
        try {
            return result.success(enterpriseCarTransferBffService.getEnterpriseCarTransfer(vin));
        } catch (BizException e) {
            logger.error("[getEnterpriseCarTransfer]过户详情异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[getEnterpriseCarTransfer]过户详情异常:", e);
            result.error("过户详情异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "企业订单首付")
    @PostMapping(value = "/pay")
    @OptionalLogConfig(methods = "企业订单首付")
    public ResponseResult<Boolean> createPay(@RequestBody @Valid EnterprisePayCreateVo createVo,
                                             BindingResult bindingResult) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        try {
            boolean rst = orderEnterpriseBffService.createPay(createVo);
            result = rst ? result.success(rst) : result.error("企业订单首付失败");
        } catch (BizException e) {
            logger.error("[createPay]企业订单首付异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[createPay]企业订单首付异常", e);
            result.error("企业订单首付失败");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "企业订单还款")
    @PostMapping(value = "/repay")
    @OptionalLogConfig(methods = "企业订单还款")
    public ResponseResult<Boolean> createRepay(@RequestBody @Valid EnterprisePayCreateVo createVo,
                                               BindingResult bindingResult) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        try {
            boolean rst = orderEnterpriseBffService.createPay(createVo);
            result = rst ? result.success(rst) : result.error("企业订单还款失败");
        } catch (BizException e) {
            logger.error("[createRepay]企业订单还款异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[createRepay]企业订单还款异常", e);
            result.error("企业订单还款失败");
        }
        return result;
    }

    /**
     * 验证企业订单还款
     *
     * @param orderCode  订单号
     * @param scheduleId 排期id
     * @return
     */
    @Override
    @ApiOperation(value = "验证企业订单还款")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderCode", value = "订单号", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "scheduleId", value = "排期id", required = true, dataType = "Integer"),
    })
    @GetMapping(value = "/verify/repay")
    public ResponseResult<EnterpriseScheduleVerifyVo> getVerifyRepay(@RequestParam Long orderCode,
                                                                     @RequestParam Integer scheduleId) {
        ResponseResult<EnterpriseScheduleVerifyVo> result = new ResponseResult<>();
        try {
            result.success(orderEnterpriseBffService.getVerifyRepay(orderCode, scheduleId));
        } catch (BizException e) {
            logger.error("[getVerifyRepay]验证企业订单还款异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[getVerifyRepay]验证企业订单还款异常", e);
            result.error("验证企业订单还款失败");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "查询订单和车辆信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderCode", value = "订单号", required = true, dataType = "Long"),
    })
    @GetMapping(value = "/info")
    public ResponseResult<EnterpriseAndCarVo> getEnterprise(Long orderCode) {
        ResponseResult<EnterpriseAndCarVo> result = new ResponseResult<>();
        try {
            result.success(orderEnterpriseBffService.getEnterprise(orderCode));
        } catch (BizException e) {
            logger.error("[getEnterprise]查询订单和车辆信息异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[getEnterprise]查询订单和车辆信息异常", e);
            result.error("查询失败");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "续租校验")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderCode", value = "订单号", required = true, dataType = "Long"),
    })
    @GetMapping(value = "/check/renew")
    public ResponseResult<Boolean> checkRenew(Long orderCode) {

        logger.info("校验续租:{}", orderCode);

        ResponseResult<Boolean> result = new ResponseResult<>();
        try {
            List<EnterpriseScheduleDto> list = enterpriseScheduleService.getSchedule(orderCode);
            if (list==null || list.size()<0){
                return result.success(true);
            }
            for (EnterpriseScheduleDto dto : list) {
                if(dto!=null && dto.getState()==0){
                        return result.success(false);
                }
            }
        } catch (BizException e) {
            logger.error("校验续租是否有未还款异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("校验续租是否有未还款异常", e);
            result.error("查询失败");
        }
        return result.success(true);
    }

    @Override
    @ApiOperation(value = "起租确认")
    @PostMapping(value = "/lease/confirm")
    @OptionalLogConfig(methods = "起租确认")
    public ResponseResult<Boolean> leaseConfirm(@RequestBody @Valid EnterpriseLeaseConfirmCreateVo createVo, BindingResult bindingResult) {
        ResponseResult<Boolean> result = new ResponseResult<>();
        try {
            boolean rst = orderEnterpriseBffService.leaseConfirm(createVo);
            result = rst ? result.success(rst) : result.error("起租确认失败失败");
        } catch (BizException e) {
            logger.error("[create]起租确认异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("[create]起租确认异常", e);
            result.error("起租确认失败");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "车辆企业订单列表")
    @GetMapping(value = "/list/carId")
    @OptionalLogConfig(methods = "车辆企业订单列表")
    public ResponseResult<List<OrderEnterpriseListVo>> orderEnterpriseListByCarId(@RequestParam Integer carId) {
        ResponseResult<List<OrderEnterpriseListVo>> result = new ResponseResult<>();
        try {
            result.success(orderEnterpriseBffService.getListByCarId(carId));
        } catch (BizException e) {
            logger.error("查询企业订单异常:", e);
            result.error(e.getExceptionCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("查询企业订单异常:", e);
            result.error("查询企业订单异常信息异常");
        }
        return result;
    }
}
